/* 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

Title : com.Angaliwa.NetStreamHandler
Author : Dave Jansen -- Zenitum
URL : http://www.zenitum.com/

Description : The NetStreamHandler module of Angaliwa,
			  which allows you to easily connectm create
			  and use NetStreams.

Created : May 1st, 2009

History		: v1.2.090811 Completely rewrote most of the 
			  module to support the new Angaliwa modules.

			  v1.1 Undocumented update.
			  
			  v1.0.090301 Initial version

Version: 1.2
Compatibility: 1.2

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
*/

package com.Angaliwa
{
	import flash.events.TimerEvent;
	import flash.media.Camera;
	import flash.media.Microphone;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.utils.Timer;
	
	public class NetStreamHandler{
		private var nc:NetConnection;
		private var ns:NetStream;
		
		/*
		 * Summary:      	Initializes the new NetStreamHandler
		 					instance. Requires a NetConnection, or
		 					access to Angaliwa's FMSConnect module.
		 					Originally called 'Set'.
		 * Return:       	Void
		 * Version:   	 	1.2
		 * Compatibility:	1.2
		 */
		public function NetStreamHandler(nc:*){
			if(nc is NetConnection)
				this.nc = nc;
			else if(nc is FMSConnect)
				this.nc = nc.g();
			else trace('NetStreamHandler: Incorrect NetConnection provided.');
		}
		
		public function a(name:String, record:Boolean=false, cam:Camera=undefined, mic:Microphone=undefined):NetStreamHandler{
			if(ns) close();
			
			// Start a new NetStream session
			ns = new NetStream(this.nc);
			
			if(cam) ns.attachCamera(cam);
			if(mic) ns.attachAudio(mic);
			
			var timer:Timer = new Timer(100);
			timer.addEventListener(TimerEvent.TIMER, function(e:TimerEvent):void{
				// For testing purposes. It seems that 'touching' the 'time'
				// variable keeps the connection alive. Yeah. Exactly.
				var t:Number = ns.time;
			});
			timer.start();
			
			start(name,record);
			
			return this;
		}
		
		public function g(name:String, cam:Boolean=true, mic:Boolean=false):NetStream{
			if(!ns){
				ns = new NetStream(this.nc);
				trace('Adding a new netStream connection to play back '+name);
			}
			ns.receiveVideo(cam);
			//ns.receiveAudio(mic);
			ns.bufferTime=0.1;
			ns.play(name);
			
			return ns;
		}
		
		public function start(name:String, record:Boolean=false):NetStreamHandler{
			if(ns){
				var rec:String = (record) ? "record" : "view";
				
				trace('Starting streaming session '+name);
				ns.publish(name,rec);
			}
			
			return this;
		}
		
		public function pause():NetStreamHandler{
			if(ns) ns.pause();
			
			return this;
		}
		
		public function resume():NetStreamHandler{
			if(ns) ns.resume();
			
			return this;
		}
		
		public function close():NetStreamHandler{
			if(ns) ns.close();
			
			return this;
		}
		
		
		/*** IN DEVELOPMENT ***/
		/*
        public var netStreamChecks:Array = new Array();
        public var netStreamChecking:Timer;
        
		public function NetStreamHandler(){
		}
        
        public function addToNetStreamCheck(st:NetStream):NetStream{
			if(!this.inNetStreamCheck(st)){
				this.netStreamChecks.push(st);
				
				return this.netStreamChecks[this.netStreamChecks.length-1];
			}
			
			return undefined;
        }
        
        public function removeFromNetStreamCheck(st:NetStream):void{
        	var nw:Array = new Array();
        	
        	for(var i:int=0; i < this.netStreamChecks.length; i++)
        		if(this.netStreamChecks[i] != st)
        			nw.push(this.netStreamChecks[i]);
        	
        	this.netStreamChecks = nw;
        }
        
        public function inNetStreamCheck(st:NetStream):Boolean{
        	for(var i:int=0; i < this.netStreamChecks.length; i++)
				if(this.netStreamChecks[i] == st) return true
			
			//st.addEventListener(NetStatusEvent.NET_STATUS, function(e:NetStatusEvent):void{
			//	trace('st: '+e.info.code);
			//});
			
        	return false;
        }
        
        public function startNetStreamCheck():void{
        	if(!this.netStreamChecking){
        		this.netStreamChecking = new Timer(1000);
        		this.netStreamChecking.addEventListener(TimerEvent.TIMER, checkNetStream);
        		this.netStreamChecking.start();
        	}
        }
        
        public function checkNetStream(e:TimerEvent):void{
        	//Logger.logger('Checkin netStreams..');
        	
        	for(var i:int=0; i < this.netStreamChecks.length; i++){
        		//Logger.logger('netStream #'+i+': '+this.netStreamChecks[i].time +' | '+ this.netStreamChecks[i].bufferLength);
        	}
        }
        */
	}
}